/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-12-28 11:45:36
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-12-28 12:20:02
 * @FilePath: \wire_receiver_collect_box\bsp\bsp_can.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

#include "bsp_can.h"

bsp_can_com_t can_com;

/*******************************************************************
* 函数名: bsp_can0_init()
* 功 能: 
* 输 入: 
* 输 出: 
*******************************************************************/
void bsp_can0_init(void)
{
    can_parameter_struct can_parameter;
    can_filter_parameter_struct can_filter;

    rcu_periph_clock_enable(RCU_CAN0);
    rcu_periph_clock_enable(RCU_GPIOD);

    //gpio_init(GPIOD, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
    //gpio_init(GPIOD, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_0);

    can_deinit(CAN0);

    can_parameter.time_triggered = DISABLE;
    can_parameter.auto_bus_off_recovery = DISABLE;
    can_parameter.auto_wake_up = DISABLE;
    can_parameter.auto_retrans = DISABLE;
    can_parameter.rec_fifo_overwrite = DISABLE;
    can_parameter.trans_fifo_order = DISABLE;
    can_parameter.working_mode = CAN_NORMAL_MODE;
    can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
    can_parameter.time_segment_1 = CAN_BT_BS1_5TQ;
    can_parameter.time_segment_2 = CAN_BT_BS2_3TQ;
    /* baudrate 250Kbps */
    can_parameter.prescaler = 24;
    can_init(CAN0, &can_parameter);

    can_filter.filter_number = 0;

    can_filter.filter_mode = CAN_FILTERMODE_MASK;
    can_filter.filter_bits = CAN_FILTERBITS_32BIT;
    can_filter.filter_list_high = 0x0000;
    can_filter.filter_list_low = 0x0000;
    can_filter.filter_mask_high = 0x0000;
    can_filter.filter_mask_low = 0x0000;  
    can_filter.filter_fifo_number = CAN_FIFO0;
    can_filter.filter_enable = ENABLE;
    can_filter_init(&can_filter);

    can_interrupt_enable(CAN0, CAN_INT_RFNE0);
    nvic_irq_enable(CAN0_RX1_IRQn, 1, 1);
}

/*******************************************************************
* 函数名: bsp_can0_write_bytes()
* 功 能: can0 在固定地址发送一帧数据
* 输 入: 
* 输 出: 
*******************************************************************/
void bsp_can0_write_bytes(uint8_t can_id, uint8_t *data, uint8_t len)
{
    can_trasnmit_message_struct g_transmit_message;

    g_transmit_message.tx_sfid = can_id;
    g_transmit_message.tx_efid = 0x00;
    g_transmit_message.tx_ft = CAN_FT_DATA;
    g_transmit_message.tx_ff = CAN_FF_STANDARD;
    g_transmit_message.tx_dlen = len;
    for (size_t i = 0; i < len; i++)
    {
        g_transmit_message.tx_data[i] = data[i];
    }
    can_message_transmit(CAN0, &g_transmit_message);
}

/*******************************************************************
* 函数名: bsp_can0_read_bytes()
* 功 能: 读取can0一帧数据
* 输 入: 
* 输 出: 
*******************************************************************/
uint8_t bsp_can0_read_bytes(uint8_t* data, uint8_t len)
{
    can_receive_message_struct g_receive_message;

	return 0;
}
